package server

import (
	"context"
	"fmt"
	"log"
	"net"

	"fly.org/pool"
)

type TcpServer struct {
	BaseServer
	pool *pool.WorkerPool
}

func (ts *TcpServer) Do(ctx context.Context, conn *Connection) {

}
func (ts *TcpServer) Start() {
	ts.IsRunning = true
	address := fmt.Sprintf("%s:%d", ts.Ip, ts.Port)
	listen, err := net.Listen("tcp", address)
	if err != nil {
		panic(err)
	}

	for ts.IsRunning {
		client, err := listen.Accept()
		if err != nil {
			continue
		}
		ctx := context.Background()
		conn := &Connection{conn: client}
		if ts.pool == nil {
			func() {
				defer func() {
					msg := recover()
					log.Default().Println(msg)
				}()
				ts.Do(ctx, conn)
			}()
		} else {
			ts.pool.Pulish(func(ctx context.Context) {})
		}

	}

}
